package com.chaodev.base.controller;

import cn.hutool.core.util.StrUtil;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.PlayGuideDto;
import com.chaodev.base.model.PlayGuide;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.PlayGuideService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("playGuide")
public class PlayGuideController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;
    @Autowired
    private PlayGuideService playGuideService;


    @PostMapping(value = "/page")
    @ApiOperation(value = "获取游玩攻略列表分页")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getPage(@RequestBody PlayGuideDto dto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if(dto.getPage() == null || dto.getPage() == 0
                    || dto.getRows() == null || dto.getRows() == 0){
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(playGuideService.queryPageList(dto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(dto.toString(), "获取游玩攻略列表分页", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getDataById")
    @ApiOperation(value = "获取游玩攻略对象")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getDataById(@RequestBody PlayGuide playGuide, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
//        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
//            if (!redisDao.existsKey(token)) {
//                result.setCode(ResultEnum.EREOR.getCode());
//                result.setMsg("登录超时");
//                return result;
//            }
            if (playGuide.getId() == null || "".equals(playGuide.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
//            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(playGuideService.findDataById(playGuide.getId()));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
            playGuideService.updateVisitNum(playGuide);
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(playGuide.toString(), "获取游玩攻略对象", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存游玩攻略对象")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result save(@RequestBody PlayGuide playGuide, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if(StrUtil.isEmpty(playGuide.getCUser())){
                playGuide.setCUser(userInfo.getUserId());
            }
            if(StrUtil.isEmpty(playGuide.getSendOrgCode())){
                playGuide.setSendOrgCode(userInfo.getDepartId());
            }
            if(StrUtil.isEmpty(playGuide.getSendOrgName())){
                playGuide.setSendOrgName(userInfo.getDepartName());
            }
            if(StrUtil.isEmpty(playGuide.getUpdtr())){
                playGuide.setUpdtr(userInfo.getUserId());
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            playGuide.setcUser(userInfo.getUserId());
            playGuideService.saveData(playGuide);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("操作成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(playGuide.toString(), "保存游玩攻略对象", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/del")
    @ApiOperation(value = "删除游玩攻略对象")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result delete(@RequestBody PlayGuide playGuide, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            playGuideService.deleteDataById(playGuide.getId());
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("操作成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(playGuide.toString(), "删除游玩攻略对象", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/updateVisitNum")
    @ApiOperation(value = "更新访问量")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result updateVisitNum(@RequestBody @ApiParam("id") PlayGuide tbAdvice, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (redisDao.existsKey(token)) {
                userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
                result.setCode(ResultEnum.SUCCESS.getCode());
                playGuideService.updateVisitNum(tbAdvice);
                result.setMsg("更新访问量成功");
            } else {
                result.setCode(-1);
                result.setMsg("登录超时");
            }
        } catch (Exception exception) {
            result.setCode(-1);
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(tbAdvice.toString(), "更新访问量", resultCode, logResult.toString(), userInfo, request);
            return result;
        }
    }

    @PostMapping(value = "/allPage")
    @ApiOperation(value = "获取所有游玩攻略对象")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result allPage(@RequestBody PlayGuideDto dto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
//        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
//            if (!redisDao.existsKey(token)) {
//                result.setCode(ResultEnum.EREOR.getCode());
//                result.setMsg("登录超时");
//                return result;
//            }
            if(dto.getPage() == null || dto.getPage() == 0
                    || dto.getRows() == null || dto.getRows() == 0){
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

//            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(playGuideService.queryPageAllList(dto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(dto.toString(), "获取所有游玩攻略对象", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

}
